﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;

namespace Web.admin.InnerManage
{
    public partial class AddInnerProc : System.Web.UI.Page
    {
        ContestService contestService = new ContestService();
        PhaseService phaseService = new PhaseService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PhaseList.DataSource = phaseService.getAllPhases();
                PhaseList.DataTextField = "PHA_NAME";
                PhaseList.DataValueField = "PHA_ID";
                PhaseList.DataBind();
                FillByPhase(PhaseList.SelectedValue);

                string operation = Request["OPERATION"];
                if (operation == null)
                {
                    string contestId = Request["ContestId"];
                    ContestId.Value = contestId;
                    return;
                }
                string procId = Request["PROCID"];
                if (procId == null)
                    return;

                if (operation == "Modify")
                {
                    ModifyProcedure(procId);
                }
                else if (operation == "Delete")
                {
                    DeleteProcedure(procId);
                }
            }
        }

        protected void ModifyProcedure(string procId)
        {
            ProcId.Value = procId;

            BoProcedures proc = contestService.GetProcedure(procId);

            PhaseList.DataSource = phaseService.getAllPhases();
            PhaseList.DataTextField = "PHA_NAME";
            PhaseList.DataValueField = "PHA_ID";
            PhaseList.DataBind();
            PhaseList.SelectedValue = proc.PRO_PHASES;

            txtStartTime.Text = proc.PRO_STARTTIME.ToString();
            txtEndTime.Text = proc.PRO_ENDTIME.ToString();
            txtAddress.Text = proc.PRO_ADDRESS;
            txtConductor.Text = proc.PRO_CONDUCTOR;
            txtTitle.Text = proc.PRO_CNRTITLE;
            txtMethod.Text = proc.PRO_METHOD;
            KindEditor.Text = proc.PRO_CONTENT;
        }

        protected void DeleteProcedure(string teacherId)
        {

        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            BoProcedures proc = null;
            if (ProcId.Value == "")
            {
                proc = new BoProcedures();
                proc.PRO_CONTESTS = ContestId.Value;
            }
            else
                proc = contestService.GetProcedure(ProcId.Value);

            proc.PRO_PHASES = PhaseList.SelectedValue;

            proc.PRO_STARTTIME = DateTime.Parse(txtStartTime.Text);
            proc.PRO_ENDTIME = DateTime.Parse(txtEndTime.Text);
            proc.PRO_ADDRESS = txtAddress.Text;
            proc.PRO_CONDUCTOR = txtConductor.Text;
            proc.PRO_CNRTITLE = txtTitle.Text;
            proc.PRO_METHOD = txtMethod.Text;
            proc.PRO_CONTENT = KindEditor.Text;

            if (ProcId.Value == "")
                contestService.CreateProcedure(proc);
            else
                contestService.UpdateProcedure(proc);

            Response.Redirect("ListInnerProc.aspx?id=" + proc.PRO_CONTESTS);
        }

        protected void PhaseList_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillByPhase(PhaseList.SelectedValue);
        }

        protected void FillByPhase(string phaseId)
        {
            if (ProcId.Value != "")
                return;
            BoPhases phase = phaseService.getPhase(phaseId);
            txtTitle.Text = phase.PHA_CNRTITLE;
            txtMethod.Text = phase.PHA_METHOD;
            KindEditor.Text = phase.PHA_CONTENT;
        }
    }
}